﻿
using System;
using Microsoft.Practices.Unity;

namespace UnityDemo.DemoCode
{
    //http://stackoverflow.com/questions/2813322/unity-2-0-how-to-use-resolve-with-resolveroverride
    public class DependencyAttributeDemo :IDemo
    {
        // ResolverOverride lets you pass parameters to constructor at the time of resolving the class.
        public DependencyAttributeDemo()
       { 
            var container = new UnityContainer(); 
 
            var consumer1 = container.Resolve<ServiceConsumer>(
                new PropertyOverride("Service", new ConcreteService(1)),  
                new PropertyOverride("Service1", new ConcreteService(-1))); 
            
            var consumer2 = container.Resolve<ServiceConsumer>(
                new PropertyOverride("Service", new ConcreteService(42)),  
                new PropertyOverride("Service1", new ConcreteService(-42)));

            Console.WriteLine(consumer1.Service.Val);
            Console.WriteLine(consumer1.Service1.Val); 

            Console.WriteLine(consumer2.Service.Val); 
            Console.WriteLine(consumer2.Service1.Val);

            Console.ReadLine(); 
 
        } 
    } 
 
    public interface IConcreteService
    { 
        int Val { get; set; } 
    } 

    public class ConcreteService : IConcreteService
    { 
        public int Val
        {
            get; 
            set;
        } 
 
        public ConcreteService(int val) { 
            Val = val; 
        } 
    } 
 
    public class ServiceConsumer
    { 
        [Dependency] 
        public IConcreteService Service{ get; set; } 
 
        [Dependency] 
        public IConcreteService Service1 { get; set; } 
 
    } 
} 


